-----------------------------------------------------------------------------------------
-- filename : roll_box.lua
-- description : messgaebox for showing option wheter player want roll effect or not
-----------------------------------------------------------------------------------------

--load needed libaries/scripts
local storyboard = require "storyboard"
local layer = require "layer"

-- local variables
local callback = nil
local scene = storyboard.newScene()
local playerid = 0
local diceImage = nil
local number = 0
local group = nil

function scene:createScene(event)
	print("overlay")
	callback = event.params.callback
	playerid = event.params.playerid
	group = self.view

	local background = display.newImageRect( "assets/confirmation/background.png",480,240)
	background.x = 480/2
	background.y = 320/2

	-- header image
	local header = display.newImageRect("assets/confirmation/reroll_header.png",480,30)
	header.x = 480/2
	header.y = 320/2 - 50 

	-- DiceImage
	diceImage = display.newImageRect( "assets/player-"..playerid.."/pion1.png", 63, 76 )
	diceImage.x = 480/2
	diceImage.y = 320/2 + 20
	diceImage:addEventListener("touch", diceTouch)


	group:insert(background)
	group:insert(header)
	group:insert(diceImage)
end

function diceTouch( event )
	if(event.phase == "began") then
		print("yes")
		if(number == 0) then
			number = math.random(6)
			print("number : "..number)
			-- animation dice
			

			-- animation roll
			-- WARNING : It's big size image file
			local sheetData = {
				width=480,
				height=100,
				numFrames=22,
				sheetContentWidth=480*3,
				sheetContentHeight=100*8
			}

			local mySheet = graphics.newImageSheet("assets/animation/reroll_sheet.png",sheetData)
			
			local sequenceData = {
				{
					name = "animation",
					frames = {1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,11,12,13,14,15,16,17,18,19,20,21,22,22,22,22},
					time = 3000, -- time in millisecond
					loopCount = 1,
					loopDirection = "forward"
				}
			}

			local animation = display.newSprite(mySheet, sequenceData)
			animation.x = diceImage.x
			animation.y = diceImage.y

			local animationEnded = function ( event )
				 if ( event.phase == "ended" ) then
					local thisSprite = event.target --"event.target" references the sprite
					thisSprite.parent:remove(thisSprite)
					
				elseif(event.phase == "next") then
					
					if(animation.frame == 7) then 
						local toRightComplete = function (event)
							local toLeftComplete = function(event)
								diceImage.parent:remove(diceImage)
								diceImage = nil
							end
							transition.to(diceImage, {time = 300, x = diceImage.x - 300, y = diceImage.y, alpha = 1, transition = easing.inOutExpo, onComplete=toLeftComplete})
						end

						transition.to(diceImage, {time = 200, x = diceImage.x + 100, y = diceImage.y, transition = easing.inOutExpo, onComplete=toRightComplete})
					elseif(animation.frame == 32) then
						local newDiceImage = display.newImageRect( "assets/player-"..playerid.."/pion"..number..".png", 63, 76 )
						newDiceImage.x = 480/2 + 100
						newDiceImage.y = 320/2 + 20
						newDiceImage:addEventListener("touch", diceTouch)
						transition.to(newDiceImage, {time = 300, x = newDiceImage.x - 100, y = newDiceImage.y, transition = easing.inOutExpo, onComplete=toRightComplete})
						group:insert(newDiceImage)
					end
				end
			end
			animation:addEventListener("sprite", animationEnded)
			animation:play()

			
			
			return true
		end

		if(callback ~= nil) then
			local newEvent = {
				result = 1,
				resultRoll = number
			}
			callback:callbackListen(newEvent)
		end
		storyboard.hideOverlay()
	end
	return true
end

-- add event listener
scene:addEventListener("createScene", scene)

return scene